package top.zuli.package01.class14;

public class WindowTest2 {
    public static void main(String[] args) {
        Thread t1 = new Window2();
        Thread t2 = new Window2();
        Thread t3 = new Window2();
        t1.start();
        t2.start();
        t3.start();
    }
}
class Window2 extends Thread{
    private static int ticket = 100;
    private static Object obj = new Object();
    @Override
    public void run() {
        while (true){
            //synchronized (obj) {
            //synchronized (this) { 错误 两个线程不是用的同一把锁
            synchronized (Window2.class){
                if(ticket>0){
                    try {
                        sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(getName()+"卖票,票号："+ticket);
                    ticket--;
                }else{
                    break;
                }
            }
        }
    }
}
